﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using ThoughtWorks.QRCode.Codec;

namespace YuFeng.Web.Helpers
{
    public static class QrCodeHelper
    {

        #region QRCode 公用方法

        private static QRCodeEncoder GetDefaultQrcodeEncoder()
        {
            var qrEntity = new QRCodeEncoder
            {
                QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE, //二维码编码方式
                QRCodeScale = 10, //每个小方格的宽度
                QRCodeVersion = 0, //二维码版本号
                QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M //纠错码等级
            };
            return qrEntity;
        }

        public static Bitmap GetQrCode(QRCodeEncoder qrEntity, string code)
        {
            if (qrEntity == null)
            {
                qrEntity = GetDefaultQrcodeEncoder();
            }
            Bitmap srcimage = qrEntity.Encode(code, System.Text.Encoding.UTF8);
            return srcimage;
        }

        public static Bitmap GetQrCode(string code)
        {
            var qrEntity = GetDefaultQrcodeEncoder();
            return GetQrCode(qrEntity, code);
        }

        public static Bitmap GetQrCode(string code, Color foreColor)
        {
            var qrEntity = GetDefaultQrcodeEncoder();
            qrEntity.QRCodeForegroundColor = foreColor;
            return GetQrCode(qrEntity, code);
        }

        #endregion

        public static string GetBatchQrCodeUrl(string filename, string code)
        {
            string uploadDir = ConfigHelper.Get("app.uploadfiles.dir");
            string urlGet = ConfigHelper.Get("app.uploadfiles.url.get");
            string wechatUrl = ConfigHelper.Get("app.uploadfiles.url.get");

            string qrcodePath = HttpContext.Current.Server.MapPath(uploadDir.TrimEnd('\\') + "\\qrcodes");

            if (!Directory.Exists(qrcodePath))
            {
                Directory.CreateDirectory(qrcodePath);
            }

            string filePath = qrcodePath + "\\" + filename + ".jpg";
            if (!File.Exists(filePath))
            {
                var qrCode = GetQrCode(code);
                qrCode.Save(filePath, ImageFormat.Jpeg);
            }
            return urlGet.TrimEnd('/') + "/qrcodes/" + filename + ".jpg";
        }

        public static string GetGoodTeacherQrCodeUrl(string filename, string code)
        {
            string uploadDir = ConfigHelper.Get("app.uploadfiles.dir");
            string urlGet = ConfigHelper.Get("app.uploadfiles.url.get");
            string wechatUrl = ConfigHelper.Get("app.uploadfiles.url.get");

            string qrcodePath = HttpContext.Current.Server.MapPath(uploadDir.TrimEnd('\\') + "\\goodTeacherQrcodes");

            if (!Directory.Exists(qrcodePath))
            {
                Directory.CreateDirectory(qrcodePath);
            }

            string filePath = qrcodePath + "\\" + filename + ".jpg";
            if (!File.Exists(filePath))
            {
                var qrCode = GetQrCode(code, Color.DarkOrange);
                qrCode.Save(filePath, ImageFormat.Jpeg);
            }
            return urlGet.TrimEnd('/') + "/goodTeacherQrcodes/" + filename + ".jpg";
        }
    }
}